<?php
	//if (!defined('IN_DIR')){exit('Undefined Source');}
	class DB
	{
		var	$pconnect	=	'0';
		var	$dbserver	=	'';
		var	$dbuser		=	'';
		var	$dbpass		=	'';
		var	$dbname		=	'';
		var	$query_num	=	'0';
		var 	$dbchar		=	'';
		var	$version	=	0;
		/*
		+-----------------------------------------------
		+	mySQL	:	Connect and SELECT database
		+-----------------------------------------------
		*/
		function connect()
		{
			global $pconnect,$dbserver,$dbuser,$dbpass,$dbname,$dbchar;
			$this->pconnect	=	$pconnect	;
			$this->dbserver	=	$dbserver	;
			$this->dbuser	=	$dbuser		;
			$this->dbpass	=	$dbpass		;
			$this->dbname	=	$dbname		;
			/*
			+-------------------------------------------
			+	mySQL	:	Pconnect
			+-------------------------------------------
			*/
			if ($this->pconnect)
			{
				if (!@mysql_pconnect($this->dbserver,$this->dbuser,$this->dbpass))
				{
					$this->sql_error('Can not Connect to mySQL');
				}
			}
			/*
			+-------------------------------------------
			+	mySQL	:	Connect
			+-------------------------------------------
			*/
			else
			{
				if (!@mysql_connect($this->dbserver,$this->dbuser,$this->dbpass))
				{
					$this->sql_error('Can not Connect to mySQL');
				}
			}
			/*
			+-------------------------------------------
			+	mySQL	:	Check version
			+-------------------------------------------
			*/
			$this->version();
			/*
			+-------------------------------------------
			+	mySQL	:	Set charset!
			+-------------------------------------------
			*/
			if ($this->version > '4.1')
			{
				$this->dbchar	=	in_array($dbchar,array('gbk','utf8','big5')) ? $dbchar : 'utf8';
				mysql_query("SET NAMES '".$this->dbchar."'");
			}
			/*
			+-------------------------------------------
			+	mySQL	:	SQL mode
			+-------------------------------------------
			*/
			if (mysql_get_server_info() > '5.0')
			{
				mysql_query("SET sql_mode=''");
			}
			/*
			+-------------------------------------------
			+	mySQL	:	SELECT database.
			+-------------------------------------------
			*/
			if (!mysql_SELECT_db($this->dbname))
			{
				$this->sql_error('Can not SELECT database');
			}
		}
		/*
		+-----------------------------------------------
		+	mySQL	:	close mySQL link
		+-----------------------------------------------
		*/	
		function close()
		{
			return mysql_close();
		}
		/*
		+-----------------------------------------------
		+	mySQL	:	Version		
		+-----------------------------------------------
		*/	
		function version()
		{
			$this->version = mysql_get_server_info();
		}
	/*
		+-----------------------------------------------
		+	mySQL	:	Query		
		+-----------------------------------------------
		*/		
	
		function query($sql,$force=0,$silent=0)
		{
			if ($force	&&	function_exists('mysql_unbuffered_query'))
			{
				$query=mysql_unbuffered_query($sql);
			}
			else
			{
				$query=mysql_query($sql);
			}
			if (!$query)
			{
				if (strtoupper($this->dbchar) == 'BIG5')
				{
					$sql = str_replace("\'","'",$sql);
					$query = mysql_query($sql);
					if (!$query)
					{
						$this->sql_error('mySQL Query Error',$sql,$silent);
					}
					else
					{
						$this->querys++;
					}
				}
				else
				{
					$this->sql_error('mySQL Query Error',$sql,$silent);
				}
			}
			else
			{
				$this->querys++;
			}
			return $query;
		}		
		/*
		+-----------------------------------------------
		+	mySQL	:	Query	result	force	to	free	
		+-----------------------------------------------
		*/			
		function free($query)
		{
			return mysql_free_result($query);
		}
		/*
		+-----------------------------------------------
		+	mySQL	:	Query	ID	
		+-----------------------------------------------
		*/			
		function insert_id()
		{
			return mysql_insert_id();
		}
		/*
		+-----------------------------------------------
		+	mySQL	:	SELECT	nums	FROM	server	
		+-----------------------------------------------
		*/			
		function num($query)
		{
			return mysql_num_rows($query);
		}
		/*
		+-----------------------------------------------
		+	mySQL	:	Get		array	data		
		+-----------------------------------------------
		*/			
		function row($query,$sql_type=MYSQL_ASSOC)
		{
			return mysql_fetch_array($query,$sql_type);
		}
		
		function fetch_row($query)
		{
			return mysql_fetch_row($query);
		}

		function fetch_one_array($query) {
			$result = $this->query($query);
			$record = $this->row($result);
			return $record;
		}

		function fetch_one_field($query) {
			$result = $this->query($query);
			$record = $this->row($result);
			foreach($record as $val){
              return $val;
			}
		}			
		function result($query, $row) 
		{
			$query = mysql_result($query, $row);
			return $query;
		}
		function affect() 
		{
			return mysql_affected_rows();
		}
		function select_db($dbname) 
		{
			return mysql_select_db($dbname);
		}
		/*
		+-----------------------------------------------
		+	mySQL	:	Class mysql	backup
		+-----------------------------------------------
		*/
		function escape($string)
		{
			return trim(mysql_escape_string($string));
		}
		function num_fields($query) 
		{
			return mysql_num_fields($query);
		}
		function free_result($query)
		{
			return mysql_free_result($query);
		}

		/*
		+-----------------------------------------------
		+	mySQL	:	Class	Error	info
		+-----------------------------------------------
		*/
		function error()
		{
			return mysql_error();
		}
		function errno()
		{
			return mysql_errno();
		}
		/*
		+-----------------------------------------------
		+	mySQL	:	Class	Error	info	output		
		+-----------------------------------------------
		*/		
		function sql_error($message = '', $sql = '', $silent = 0) {
		global $php_self,$timestamp,$onlineip;
		if ($sql) {
			@$fp = fopen(_ROOT.'cache/log/dberrorlog.php', 'a');
			@fwrite($fp, "<?PHP exit('Access Denied'); ?>\t$timestamp\t$onlineip\t".basename($php_self)."\t".htmlspecialchars($this->errno())."\t".str_replace(array("\r", "\n", "\t"), array(' ', ' ', ' '), trim(htmlspecialchars($sql)))."\n");
			@fclose($fp);
		}
		if (! defined ( 'IN_SQL_SILENT' )) {
			$html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">';
			$html .= '<head><title>mySQL Message</title><style type="text/css">body {margin:0px;color:#555555;font-size:12px;background-color:#efefef;font-family:Verdana} ol {margin:0px;padding:0px;} .w {width:800px;margin:100px auto;padding:0px;border:1px solid #cccccc;background-color:#ffffff;} .h {padding:8px;background-color:#ffffcc;} li {height:auto;padding:5px;line-height:22px;border-top:1px solid #efefef;list-style:none;overflow:hidden;}</style></head>';
			$html .= '<body><div class="w"><ol>';
			if ($message) {
				$html .= '<div class="h">' . $message . '</div>';
			}
			$html .= '<li>Error: ' . $this->error () . '</li>';
			$html .= '<li>Errno: ' . $this->errno () . '</li>';
			$html .= '<li>Date: ' . date ( 'Y-n-j H:i:s', time () ) . '</li>';
			$html .= '<li>SQLID: '.$sql. '</li>';
			$html .= '</ol></div></body></html>';
			if (defined ( 'IN_ISXML' )) {
				@header ( "Content-type: application/xml" );
				$html = '<?xml version="1.0" encoding="' . CHARSET . '" ?><root><![CDATA[' . $html . ']]></root>';
			}
			print $html;
			exit ();
		}
	}
}
?>